﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class TaskManagerUserDetailBLL
    {
        private static readonly IAreaDA areaDA = DataAccess.CreateAreaDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ITaskManagerUserDetailDA taskManagerUserDetailDA = DataAccess.CreateTaskManagerUserDetailDA();

        public TaskManagerUserDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public TaskManagerUserDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TaskManagerUserDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteTaskManagerUserDetail(string userID, List<string> LineID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string whLoginID = this.dataBaseID[0];
            string temp = string.Empty;
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                List<TaskManagerUserDetailInfo> taskManagerUserDetailInfoList = new List<TaskManagerUserDetailInfo>();
                TaskManagerUserDetailInfo taskManagerUserDetailInfo = null;
                foreach (string id in LineID)
                {
                    taskManagerUserDetailInfo = taskManagerUserDetailDA.GetTaskManagerUserDetailByID(dataBase, tran, userID, id, this.dataBaseID[0]);
                    taskManagerUserDetailInfoList.Add(taskManagerUserDetailInfo);
                }
                if (taskManagerUserDetailDA.DeleteTaskManagerUserDetail(dataBase, tran, userID, LineID, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < LineID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<TaskManagerUserDetailInfo>(dataBase, tran, taskManagerUserDetailInfoList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TASK_MANAGER_USER_DETAIL", this.dataBaseID[0]);
                        temp = temp + LineID[i] + ",";
                    }
                }
                tran.Commit();
                temp = temp.Substring(0, temp.Length - 1);
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public TaskManagerUserDetailInfo GetTaskManagerUserDetailByID(string UserID, string LineID)
        {
            return taskManagerUserDetailDA.GetTaskManagerUserDetailByID(UserID, LineID, this.dataBaseID[0]);
        }

        public string InsertTaskManagerUserDetail(TaskManagerUserDetailInfo taskManagerUserDetailInfo)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (!areaDA.CheckAreaIDUnique(dataBase, tran, taskManagerUserDetailInfo.AreaID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_003", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDetailInfo.AreaID });
                }
                taskManagerUserDetailInfo.CreatedDate = DateTime.Now;
                taskManagerUserDetailInfo.UpdatedDate = DateTime.Now;
                taskManagerUserDetailInfo.UpdatedBy = taskManagerUserDetailInfo.CreatedBy;
                taskManagerUserDetailDA.InsertTaskManagerUserDetail(dataBase, tran, taskManagerUserDetailInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<TaskManagerUserDetailInfo>(dataBase, tran, null, taskManagerUserDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TASK_DETAIL", this.dataBaseID[0]);
                tran.Commit();
                temp = taskManagerUserDetailInfo.UserID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string UpdateTaskManagerUserDetail(TaskManagerUserDetailInfo taskManagerUserDetailInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (!areaDA.CheckAreaIDUnique(dataBase, tran, taskManagerUserDetailInfo.AreaID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_003", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDetailInfo.AreaID });
                }
                TaskManagerUserDetailInfo oldTaskManagerUserInfo = taskManagerUserDetailDA.GetTaskManagerUserDetailByID(dataBase, tran, taskManagerUserDetailInfo.UserID, taskManagerUserDetailInfo.LineID, this.dataBaseID[0]);
                taskManagerUserDetailDA.UpdateTaskManagerUserDetail(dataBase, tran, taskManagerUserDetailInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<TaskManagerUserDetailInfo>(oldTaskManagerUserInfo, taskManagerUserDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "TASK_DETAIL", this.dataBaseID[0]);
                tran.Commit();
                return taskManagerUserDetailInfo.UserID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return "";
        }
    }
}

